package com.example.module_service.device

import android.app.AlertDialog
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.lib_common.view.view.BaseMvvmActivity
import com.example.module_service.R
import com.example.module_service.databinding.ActivityAddDeviceBinding
import com.example.module_service.databinding.ActivityDeviceBinding
import com.google.android.material.bottomsheet.BottomSheetDialog


class AddDeviceActivity : BaseMvvmActivity<ActivityAddDeviceBinding>() {
    override fun getLayoutId(): Int = R.layout.activity_add_device

    override fun initViews() {
        // 工单类型选择点击事件
        mDataBinding.parkType.setOnClickListener {
            showWorkOrderTypeBottomSheet()
        }

        // 预约时间选择点击事件
        mDataBinding.tvAppointmentTime.setOnClickListener {
            showDateSelectorBottomSheet()
        }

        // 取消按钮点击事件
        mDataBinding.btnCancel.setOnClickListener {
            finish()
        }
    }

    private fun showWorkOrderTypeBottomSheet() {
        val dialog = BottomSheetDialog(this)
        val view = LayoutInflater.from(this).inflate(R.layout.bottom_sheet_work_order_type, null)

        dialog.setContentView(view)

        // 设置选项点击事件
        view.findViewById<TextView>(R.id.tv_repair).setOnClickListener {
            mDataBinding.tvWorkOrderType.text = "维修"
            dialog.dismiss()
        }

        view.findViewById<TextView>(R.id.tv_complaint).setOnClickListener {
            mDataBinding.tvWorkOrderType.text = "投诉"
            dialog.dismiss()
        }

        view.findViewById<TextView>(R.id.tv_cleaning).setOnClickListener {
            mDataBinding.tvWorkOrderType.text = "保洁"
            dialog.dismiss()
        }

        view.findViewById<TextView>(R.id.tv_other).setOnClickListener {
            mDataBinding.tvWorkOrderType.text = "其他"
            dialog.dismiss()
        }

        view.findViewById<TextView>(R.id.tv_yes).setOnClickListener {
            // mDataBinding.tvWorkOrderType.text = ""
            dialog.dismiss()
        }
        view.findViewById<TextView>(R.id.tv_cancel).setOnClickListener {
            //mDataBinding.tvWorkOrderType.text = "其他"
            dialog.dismiss()
        }
        dialog.show()
    }

    private fun showDateSelectorBottomSheet() {
        val dialog = BottomSheetDialog(this)
        val view = LayoutInflater.from(this).inflate(R.layout.bottom_sheet_date_selector_grid, null)

        dialog.setContentView(view)

        // 设置取消按钮点击事件
        view.findViewById<TextView>(R.id.tv_cancel1).setOnClickListener {
            dialog.dismiss()
        }

        // 设置确定按钮点击事件
        view.findViewById<TextView>(R.id.tv_confirm1).setOnClickListener {
            // 获取选中的日期并处理
            val year = view.findViewById<TextView>(R.id.tv_year_0).text.toString()
            val month = view.findViewById<TextView>(R.id.tv_month_0).text.toString()
            val day = view.findViewById<TextView>(R.id.tv_day_0).text.toString()

            mDataBinding.tvAppointmentTime.text = "${year}年${month}月${day}日"
            dialog.dismiss()
        }

        // 设置日期选择逻辑
        for (i in 0 until 7) {
            val yearView = view.findViewById<TextView>(R.id.tv_year_0 + i)
            val monthView = view.findViewById<TextView>(R.id.tv_month_0 + i)
            val dayView = view.findViewById<TextView>(R.id.tv_day_0 + i)

            yearView.setOnClickListener {
                // 选择年份
                selectYear(yearView, monthView, dayView)
            }

            monthView.setOnClickListener {
                // 选择月份
                selectMonth(yearView, monthView, dayView)
            }

            dayView.setOnClickListener {
                // 选择日期
                selectDay(yearView, monthView, dayView)
            }
        }

        dialog.show()
    }

    private fun selectYear(yearView: TextView, monthView: TextView, dayView: TextView) {
        // 这里可以添加年份选择逻辑
        // 例如：弹出年份选择对话框
        val years = arrayOf("2015年", "2016年", "2017年", "2018年", "2019年", "2020年", "2021年")

        val dialog = AlertDialog.Builder(this)
            .setTitle("选择年份")
            .setItems(years) { _, which ->
                yearView.text = years[which]
            }
            .create()

        dialog.show()
    }

    private fun selectMonth(yearView: TextView, monthView: TextView, dayView: TextView) {
        // 这里可以添加月份选择逻辑
        // 例如：弹出月份选择对话框
        val months = arrayOf("1月", "2月", "3月", "4月", "5月", "6月", "7月")

        val dialog = AlertDialog.Builder(this)
            .setTitle("选择月份")
            .setItems(months) { _, which ->
                monthView.text = months[which]
            }
            .create()

        dialog.show()
    }

    private fun selectDay(yearView: TextView, monthView: TextView, dayView: TextView) {
        // 这里可以添加日期选择逻辑
        // 例如：弹出日期选择对话框
        val days = arrayOf("1日", "2日", "3日", "4日", "5日", "6日", "7日")

        val dialog = AlertDialog.Builder(this)
            .setTitle("选择日期")
            .setItems(days) { _, which ->
                dayView.text = days[which]
            }
            .create()

        dialog.show()
    }

    override fun getActivityState(state: Bundle) {
        // 保存状态
    }

    override fun saveActivityState(outState: Bundle) {
        // 保存活动状态
    }
}
